/*
 * Copyright (c) 2018. cangoonline.net All Rights Reserved.
 * 类名称：MailSaverImpl.java
 * 创建人：韩伟
 * 联系方式：hanwei@cangoonline.com
 */
package com.cango.mail.impl;

import com.cango.framework.base.dao.IBaseDao;
import com.cango.mail.CangoMail;
import com.cango.mail.MailSaver;
import com.cango.mail.util.TemplateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Author: hanwei
 * CreateDate: 2019/9/11
 */
@Service
public class MailSaverImpl implements MailSaver {



    @Resource(name = "basedao")
    private IBaseDao baseDao;

    @Override
    public void addMail(CangoMail mail) {
        baseDao.insert("mail.insertMail",mail);
    }

    @Override
    public boolean checkTemplate(String code) {
        HashMap<String,Object> map = baseDao.queryObjectById("mail.selectTemplate",code);
        if(null == map || map.isEmpty()){
            return false;
        }
        return true;
    }

    @Override
    public String[] applyTemplate(String templateCode, Map<String,Object> subjectParams, Map<String,Object> contentParams) {
        HashMap<String,Object> map = baseDao.queryObjectById("mail.selectTemplate",templateCode);
        if(null == map){
            return null;
        }

        return new String[]{TemplateUtils.applyTemplate(map.get("subject_template").toString(),subjectParams)
                ,TemplateUtils.applyTemplate(map.get("content_template").toString(),contentParams)};
    }

    @Override
    public String getKey(String clientCode) {
        return baseDao.queryForObject("mail.getKeyByclient",clientCode);
    }

    @Override
    public String getAuthCode(String clientCode) {
        return baseDao.queryForObject("mail.getAuthCodeByclient",clientCode);
    }
}
